using System;
using System.IO;

namespace Anarchy.FS
{	
	public class GenericFS : FSWrapper
	{

		// Variables
		private String Location;
        private String ThumbLocation; 

		// Constructor
		public GenericFS(String _Location, String _ThumbLocation)
		{
			
			//..
			Location = _Location;
            ThumbLocation = _ThumbLocation;
		
		}
		
		// -- FS Stuff --
		
		// Exists
		public override bool Exists(ImageInfo _Img)
		{
		
			// Return whether it exists!
			return File.Exists(Location + _Img.Path + _Img.FileName);
		
		}
		
        // Thumb Exists
        public override bool ThumbExists(ImageInfo _Img)
        {
            
            // ..
            if (File.Exists(ThumbLocation + _Img.Path + _Img.FileName))
                return true;
            else
                return false;

        }

		// Stream!
        public override FileStream GetStream(ImageInfo _Img)
		{
		
			// If file exists.
            if (Exists(_Img))
                // Return file-stream for actual file
                return File.Open(Location + _Img.Path + _Img.FileName, FileMode.Open);
            else
                // For now throw exception, in future return an "Unavailable" image.
                throw new NotImplementedException("Requested image doesn't exist. In future, we should return an \"Unavailable\" image here.");
		}

        // Thumbnail's Stream!
        public override FileStream GetThumbStream(ImageInfo _Img)
        {
               
            // If file exists
            if (ThumbExists(_Img))
                // Return the file-stream for the thumbnail
                return File.Open(ThumbLocation + _Img.Path + _Img.FileName, FileMode.Open);
            else
                // Throw exception!
                throw new NotImplementedException("Requested Thumbnail doesn't exist. In future, we should generate one and cache here.");

        }
		
		// Add - Copy from a location (likely to be incoming) to the specified place
        public override void Add(ImageInfo _Img)
		{
		
			// Move
            String One = _Img.SecondPath + _Img.FileName;
            String Two = Location + _Img.Path + _Img.FileName;
			File.Copy(One, Two);
		
		}
		
		// Delete file (We'll stick in a recycling bin for now.
        public override void Delete(ImageInfo _Img)
		{
		
			// Move to Recycling
            File.Move(Location + _Img.Path + _Img.FileName, Location + "Trash/" + _Img.FileName);
		
		}
		
        // Recursively scan directories to get files
        public override DirectoryInfo GetList(string BaseDir)
        {
            
            // Check base-dir exists
            if (!Directory.Exists(Location + BaseDir))
                throw new DirectoryNotFoundException("Attempted to scan recursively for files a non-existant directory. THIS NEEDS FIXING.");

            // Get the BaseDir's name (Like if its "one/two/three/" get "three"
            String[] _Split = BaseDir.Split('/');
            String Name = _Split[_Split.Length - 1];

            // Scan!
            return ScanDir(Name, Location + BaseDir);

        }

        // While in directory
        private DirectoryInfo ScanDir(String Name, String FullPath)
        {

            // New Directory object
            DirectoryInfo _Dir = new DirectoryInfo();

            // Name & Full Path -- Add Relative path in for good measure
            _Dir.Name = Name;
            _Dir.FullPath = FullPath;
            _Dir.RelativePath = FullPath.Substring(Location.Length, FullPath.Length - Location.Length); // Ugly. JAHAHA

            // Add Files
            foreach (String File in Directory.GetFiles(FullPath))
                _Dir.AddFile(GetName(File));

            // Add Subdirectories Recursively.
            foreach (String Dir in Directory.GetDirectories(FullPath))
            {

                // Add to the 
                _Dir.AddSubDirectory(ScanDir(GetName(Dir), Dir));

            }

            // Return
            return _Dir;

        }

        // Gets a dir/File name from a full path
        private String GetName(String FullPath)
        {

            // Split..
            String[] _Split = FullPath.Split('\\');

            return _Split[_Split.Length - 1];

        }

        // Get Image's Filesize and edit the Img object
        public override void GetFileSize(ref ImageInfo Img)
        {
            throw new NotImplementedException();
        }

	}
}
